package org.helgoboss.janana_tray_background_operation_notification;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.helgoboss.janana_tray.JananaTray;
import org.helgoboss.janana_tray.JananaTrayAddOn;
import java.awt.TrayIcon;
import org.helgoboss.background_operation_notification.BackgroundOperationObserver;
import org.helgoboss.background_operation_notification.BackgroundOperation;

public class JananaTrayBackgroundOperationNotification implements BundleActivator, JananaTrayAddOn, BackgroundOperationObserver {
    
    private JananaTray jananaTray;
    
    public void start(BundleContext context) {
    	context.registerService(JananaTrayAddOn.class.getName(), this, null);
        context.registerService(BackgroundOperationObserver.class.getName(), this, null);
    }
    
    public void stop(BundleContext context) {
    }

	public void addToJananaTray(JananaTray tray) {
        jananaTray = tray;
	}

	public void removeFromJananaTray(JananaTray tray) {
        jananaTray = null;
	}
    
    
    public void backgroundOperationStarted(BackgroundOperation operation) {
        notify(operation.getTitle(), "Starting: " + operation.getDescription());
    }
    
    public void backgroundOperationStopped(BackgroundOperation operation) {
        notify(operation.getTitle(), "Stopping: " + operation.getDescription());
    }
    
    public void backgroundOperationUpdated(BackgroundOperation operation) {
        notify(operation.getTitle(), "Updating: " + operation.getDescription());
    }
    
    private void notify(String caption, String text) {
        if (jananaTray != null) {
            jananaTray.getIcon().displayMessage(caption, text, TrayIcon.MessageType.INFO);
        }
    }
    
}
